﻿using System;
using System.Data;
using MySql.Data;
using MySql.Data.MySqlClient;
using System.Configuration;

public class SQLHelper
{
    private static string connectionString = ConfigurationManager.ConnectionStrings["mysql"].ConnectionString;


    #region PrepareCommand

    private static void PrepareCommand(MySqlConnection conn, MySqlTransaction trans, MySqlCommand cmd, CommandType cmdType, string cmdText, MySqlParameter[] cmdParms)
    {
        if (conn.State != ConnectionState.Open)
            conn.Open();

        cmd.Connection = conn;
        cmd.CommandText = cmdText;

        if (trans != null)
            cmd.Transaction = trans;

        cmd.CommandType = cmdType;

        if (cmdParms != null)
        {
            foreach (MySqlParameter parm in cmdParms)
                cmd.Parameters.Add(parm);
        }
    }
    #endregion

    #region ExecuteNonQuery

    public static int ExecuteNonQuery(string cmdText)
    {
        return ExecuteNonQuery(cmdText, null);
    }

    public static int ExecuteNonQuery(string cmdText, params MySqlParameter[] cmdParms)
    {
        MySqlCommand cmd = new MySqlCommand();

        using (MySqlConnection conn = new MySqlConnection(connectionString))
        {
            PrepareCommand(conn, null, cmd, CommandType.Text, cmdText, cmdParms);
            int val = cmd.ExecuteNonQuery();
            cmd.Parameters.Clear();
            return val;
        }
    }

    //public static int ExecuteNonQuery(MySqlTransaction trans, CommandType cmdType, string cmdText, params MySqlParameter[] cmdParms)
    //{
    //    MySqlCommand cmd = new MySqlCommand();

    //    PrepareCommand(trans.Connection, trans, cmd, cmdType, cmdText, cmdParms);
    //    int val = cmd.ExecuteNonQuery();
    //    cmd.Parameters.Clear();
    //    return val;
    //}
    #endregion

    #region ExecuteScalar

    public static object ExecuteScalar(string cmdText)
    {
        return ExecuteScalar(cmdText, null);
    }

    public static object ExecuteScalar(string cmdText, params MySqlParameter[] cmdParms)
    {
        MySqlCommand cmd = new MySqlCommand();

        using (MySqlConnection connection = new MySqlConnection(connectionString))
        {
            PrepareCommand(connection, null, cmd, CommandType.Text, cmdText, cmdParms);
            object val = cmd.ExecuteScalar();
            cmd.Parameters.Clear();
            return val;
        }
    }

    #endregion

    //#region ExecuteReader

    //public static MySqlDataReader ExecuteReader(string connectionString, CommandType cmdType, string cmdText, params MySqlParameter[] cmdParms)
    //{
    //    MySqlCommand cmd = new MySqlCommand();
    //    MySqlConnection conn = new MySqlConnection(connectionString);

    //    try
    //    {
    //        PrepareCommand(conn, null, cmd, cmdType, cmdText, cmdParms);
    //        MySqlDataReader dr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
    //        cmd.Parameters.Clear();
    //        return dr;
    //    }
    //    catch
    //    {
    //        conn.Close();
    //        throw;
    //    }
    //}
    //#endregion

    #region ExecuteDataSet

    public static DataSet ExecuteDataSet(string cmdText)
    {
        return ExecuteDataSet(cmdText, null);
    }

    public static DataSet ExecuteDataSet(string cmdText, params MySqlParameter[] cmdParms)
    {
        MySqlCommand cmd = new MySqlCommand();

        using (MySqlConnection conn = new MySqlConnection(connectionString))
        {
            PrepareCommand(conn, null, cmd, CommandType.Text, cmdText, cmdParms);
            MySqlDataAdapter da = new MySqlDataAdapter(cmd);
            DataSet ds = new DataSet();
            da.Fill(ds);
            conn.Close();
            cmd.Parameters.Clear();
            return ds;
        }
    }
    #endregion

}
